package input;

import gameStates.absGamesStates.gui.AbsChatState;
import main.AbsInitGame;
import main.InitGame;
import main.listeners.listenerClasses.GameListener;
import main.listeners.listenerClasses.gui.ChatActivatedEvent;
import main.listeners.listenerClasses.gui.ConsoleToggleEvent;
import main.listeners.listenerClasses.gui.GUIListener;
import main.listeners.listenerClasses.gui.IngameChangedEvent;
import main.listeners.listenerClasses.gui.ScoreBoardToggleEvent;
import main.listeners.listenerClasses.gui.SpawnMenuChangedEvent;

import com.jme.input.KeyInput;
import com.jme.input.controls.GameControl;
import com.jme.input.controls.binding.KeyboardBinding;
import com.jme.input.controls.controller.ActionChangeController;
import com.jme.input.controls.controller.ControlChangeListener;
import com.jme.scene.Node;

public class BasicInput extends NodeInputHandler {
	
	private GameControl menuControl, consoleControl, abortLoadingControl;
	private AbsChatState chatState;
	private boolean consoleActive, initialized;
	
	public BasicInput(Node rootNode) {
		super(rootNode);
		
		initControls();
	}
	
	private void initControls() {
		menuControl = manager.addControl("Exit to Menu");
		menuControl.addBinding(new KeyboardBinding(KeyInput.KEY_ESCAPE));
		consoleControl = manager.addControl("OpenClose Console");
		consoleControl.addBinding(new KeyboardBinding(KeyInput.KEY_END));
		abortLoadingControl = manager.addControl("Abort Loading");
		abortLoadingControl.addBinding(new KeyboardBinding(KeyInput.KEY_ESCAPE));
		
		AbsInitGame init = InitGame.get();
		init.addGUIListener(new GUIListener() {
			@Override
			public void chatActivated(ChatActivatedEvent event) {
				setChatState(chatState);
			}
			
			@Override
			public void ingameChanged(IngameChangedEvent event) {	}
			@Override
			public void spawnMenuChanged(SpawnMenuChangedEvent event) {}
			@Override
			public void scoreBoardToggled(ScoreBoardToggleEvent event) {}
			@Override
			public void consoleToggled(ConsoleToggleEvent event) {}
		});
		
		init.addGameListener(new GameListener() {
			@Override
			public void gameStarted() {
				initialized = true;
			}
			
			@Override
			public void gameFinished() {
				initialized = false;
			}
			
			@Override
			public void gameDestroyed() {
				initialized = false;
			}
		});
		
		ActionChangeController menuAction = new ActionChangeController(menuControl, new ControlChangeListener() {
			@Override
			public void changed(GameControl control, float oldValue, float newValue, float time) {
				if(InitGame.get().isChangingGraphics()) return;
				
				if(newValue == 1f) {
					if(chatState != null && chatState.isActive()) {
						chatState.setActive(false);
						return;
					}
					
					if(!initialized) return;
					InitGame.get().changeIngame(!InitGame.get().isIngame());
				}
			}
		});
		rootNode.addController(menuAction);
		
		ActionChangeController consoleAction = new ActionChangeController(consoleControl, new ControlChangeListener() {
			@Override
			public void changed(GameControl control, float oldValue, float newValue, float time) {
				if(newValue == 1f) {
					InitGame.get().toggleConsole(!consoleActive);
					consoleActive = !consoleActive;
				}
			}
		});
		rootNode.addController(consoleAction);
		
		ActionChangeController abortAction = new ActionChangeController(abortLoadingControl, new ControlChangeListener() {
			@Override
			public void changed(GameControl control, float oldValue, float newValue, float time) {
				if(newValue == 1f) InitGame.get().abortLoadingThread();
			}
		});
		rootNode.addController(abortAction);
	}
	
	private void setChatState(AbsChatState chatState) {
		this.chatState = chatState;
	}
}